package com.itgaoshu.hospitalproject.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itgaoshu.hospitalproject.bean.Baoque;
import com.itgaoshu.hospitalproject.bean.Drugstore;
import com.itgaoshu.hospitalproject.bean.Record;
import com.itgaoshu.hospitalproject.bean.Ypharmacy;
import com.itgaoshu.hospitalproject.service.OutinstoreService;
import com.itgaoshu.hospitalproject.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
//实现出库的功能
//补给里面可以出库，药房增加，报缺表减少
//出库里面可以出库，药房增加，报缺表减少
@Controller
@RequestMapping("chuku")
public class OutinstoreController {
    @Autowired
    private OutinstoreService outinstoreService;
    @Autowired
    private RecordService recordService;
    //补给
    @RequestMapping("updatedrugnumber")
    @ResponseBody
    public Object chukudan(Drugstore drugstore, Ypharmacy ypharmacy, Record record) {
        int  addresult=0;
        //Drugstore里减掉数量
        int updatedrugnumber = outinstoreService.deleteDrugsByshuliang(drugstore);
        //查看该药品库存数
        int seldrugnamenum = outinstoreService.seldrugnamenum(drugstore);
        if (seldrugnamenum == 1) {
            //查看一下数量是否为1，说明该药品库存耗尽，直接删掉该药品
            int deldrugnamenum = outinstoreService.deleteDrugsByName(drugstore);
        }
        if (updatedrugnumber == 1) {//如果出库成功
            Integer addjilu = recordService.addjilu(record);
            System.out.println("添加补给记录成功" + addjilu);
        }
        //库房里的操作完成
        //药房里增加
       /* //查看药房里是否有该药，没有的话增加该药品
        int selresult=outinstoreService.selpharymacyname(ypharmacy);
        //有该药的话增加数量
        if(selresult==1){*/
             addresult= outinstoreService.uppharmacynumber(drugstore);
        //药房操作完成
        int selbaoqueName = outinstoreService.selbaoqueName(ypharmacy);//查询正在出库的药 有没有与报缺表冲突
        if(selbaoqueName==1){
            //检查报缺数量是否为最后一条
            int bauqueresult=outinstoreService.selbaoquenamenum(ypharmacy);
            if(bauqueresult==1){
                //如果为数量为1，直接删除此报缺
                outinstoreService.delbaoquenamenum();
            }
            //如果数量不为1，数量减少
            if(bauqueresult!=1){
                outinstoreService.upbaoquenumber1(ypharmacy);
            }
        }
        return addresult;
    }

    //查看报缺单
    @RequestMapping("selbaoquedan")
    @ResponseBody
    public Object selbaoquedan(Baoque baoque, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Baoque> selbaoquedan = outinstoreService.selbaoquedan();
        PageInfo pageInfo = new PageInfo(selbaoquedan);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }

    //查询报缺表数据查询对应的 仓库数据
    @RequestMapping("selbaoquedrugname")
    @ResponseBody
    public Object selbaoquedrugname(Drugstore drugstore, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Drugstore> drugstores = outinstoreService.selbaoquedrugname(drugstore);//根据名字查询仓库药品
        PageInfo pageInfo = new PageInfo(drugstores);
        Map<String, Object> drugstoresData = new HashMap<String, Object>();
        drugstoresData.put("code", 0);
        drugstoresData.put("msg", "");
        drugstoresData.put("count", pageInfo.getTotal());
        drugstoresData.put("data", pageInfo.getList());
        return drugstoresData;
    }

    //出库里面可以出库，药房增加，报缺表减少
    @RequestMapping("addpharmacy")
    @ResponseBody
    public Object addpharmacy(Ypharmacy ypharmacy, Drugstore drugstore, Record record) {
        int deleteshuliangresult=0;
        //检查库存数量是否为1
        int num=outinstoreService.seldrugnamenum(drugstore);
        //如果为1，删除该药品
        if(num==1){
            int deleteresult=outinstoreService.deleteDrugsByName(drugstore);
        }
        //如果不为1
        if(num!=1){
            //库存减少
             deleteshuliangresult=outinstoreService.deleteDrugsByshuliang(drugstore);
        }
        //检查药房是否有该药品
        int selresult=outinstoreService.selpharymacyname(ypharmacy);
        //如果有该药品，则增加数量
        if(selresult==1){
            outinstoreService.uppharymacy(ypharmacy);
        }
        //如果无该药品，则增加该药品
        if(selresult!=1){
            outinstoreService.addTopharmacy(ypharmacy);
        }
        int selbaoqueName = outinstoreService.selbaoqueName(ypharmacy);//查询正在出库的药 有没有与报缺表冲突
        if(selbaoqueName==1){
            //检查报缺数量是否为最后一条
            System.out.println("drugstore的值为"+drugstore);
            int bauqueresult=outinstoreService.selbaoquenamenum(ypharmacy);
            if(bauqueresult==1){
                //如果为数量为1，直接删除此报缺
                outinstoreService.delbaoquenamenum();
            }
            //如果数量不为1，数量减少
            if(bauqueresult!=1){
                outinstoreService.upbaoquenumber1(ypharmacy);
            }
        }

        return deleteshuliangresult;
    }
}